const WXAPI = require('apifm-wxapi')

Page({
  onLoad: function(options) {
    // 果商不支持代卖
    if(wx.getStorageSync('usertype') == 8){
      wx.showModal({
        title: '提示',
        content: '果商暂不支持代卖',
        showCancel: false,
        success: function (res) {
          wx.redirectTo({
            url: '/pages/seller/index',
          })
        }
      })
    }
    console.log("获取代卖列表")
    this.getSellRequestList();
  },

  // 申请出库
  async stockOut(e) {
    var stockId = e.currentTarget.dataset.id;
    var fruitName = e.currentTarget.dataset.name;
    console.log("申请出库，库存Id："+stockId+" - "+fruitName)
    wx.navigateTo({
      url: "/pages/seller/stock-out/index?stockId="+stockId+"&fruitName="+fruitName
    })
  },

  /**
   * 获取代卖信息
   */
  async getSellRequestList() {
    console.log('getting sell request list')
    const res = await WXAPI.getSellRequestList({"userId":wx.getStorageSync('userid')});
    console.log("代卖信息获取结果："+JSON.stringify(res))
    if(res.code == 20000){
      this.setData({
        sellRequestList: res.data
      });
    }
  },

  // 接收代卖确认
  async accept(e) {
    var that = this;
    console.log(JSON.stringify(e.currentTarget.dataset))
    var fruitSellRequestId = e.currentTarget.dataset.id;
    var fruitName = e.currentTarget.dataset.fruitname;
    
    console.log("申请代卖，库存Id："+fruitSellRequestId)
    wx.navigateTo({
      url: "/pages/seller/on-shelf/index?sellRequestId="+fruitSellRequestId+"&fruitName="+fruitName
    })
  },

  // 取消代卖
  async cancel(e) {
    var fruitSellRequestId = e.currentTarget.dataset.id;
    console.log("取消代卖，代卖Id："+fruitSellRequestId)
    const res = await WXAPI.cancelSellRequest({'sellRequestId':fruitSellRequestId});
    console.log("代卖取消结果："+JSON.stringify(res))
    if(res.code == 20000){
      wx.showModal({
        title: '成功',
        content: '取消代卖申请成功',
        showCancel: false
      })
      this.getSellRequestList();
    }else{
      wx.showModal({
        title: '失败',
        content: '取消失败',
        showCancel: false
      })
    }
  }
})